package com.xuecheng.search.listener;

import com.xuecheng.search.service.SearchService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

// 课程发布的就监听器
@Component
@Slf4j
public class CoursePubListener {


    @Autowired
    private SearchService searchService;

    // 获取课程发布id
    @RabbitListener(queues = "content.queue")
    public void listenCoursePub(Long coursePubId){
        log.info("课程发布id：{}",coursePubId);

        try {
            // 同步索引库
            searchService.saveDocument(coursePubId);
        } catch (IOException e) {
            log.error("课程发布id：{}",coursePubId);
        }
    }

    // 删除
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("content.queue1"),
            exchange = @Exchange(value = "content-exchange", type = ExchangeTypes.TOPIC),
            key = "course.pub1"
    ))
    public void removeByCourseId(Long courseId) throws IOException {
        searchService.removeByCourseId(courseId);
    }



}